package com.gzdh.tang.ali.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.config.ali.AliPayUtil;
import com.gzdh.tang.modu.entity.Fk;
import com.gzdh.tang.modu.service.FkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v2/test")
public class AliTestController {

    @GetMapping("test")
    public Result<Object> test() throws Exception {
        Result<Object> result = new Result<>();

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                AliPayUtil.getAppid(),
                AliPayUtil.getPrivateKey(), "json", "GBK", AliPayUtil.getPublicKey(), "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//
//        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", System.currentTimeMillis());
//        bizContent.put("total_amount", 1);
//        bizContent.put("subject", "支付测试");
//        bizContent.put("product_code", "GENERAL_WITHHOLDING");
//
////        代扣
//        JSONObject agreementParams = new JSONObject();
//        agreementParams.put("agreement_no", "20235631981035412442");
//        bizContent.put("agreement_params", agreementParams);
//
//// 业务信息
//        JSONObject businessParams = new JSONObject();
//        businessParams.put("assignJointAccountId", "2088420029171855");
//        businessParams.put("enterprisePayAmount", 0);
//        bizContent.put("business_params", businessParams);
//
//// 扩展信息
//        JSONObject extendParams = new JSONObject();
//        extendParams.put("pdSubBizScene", "enterprisePay");
//        bizContent.put("extend_params", extendParams);
//
//// 返回参数选项
//        JSONArray queryOptions = new JSONArray();
//        queryOptions.add("enterprise_pay_info");
//        bizContent.put("query_options", queryOptions);
//        request.setBizContent(bizContent.toString());
//        AlipayTradePayResponse response = alipayClient.execute(request, null, "202308BBd127680e0ea34391a01ac5700eddeF66");
//        System.out.println(response.toString());
//        if (response.isSuccess()) {
//            System.out.println("调用成功");
//        } else {
//            System.out.println("调用失败");
//        }

        request.setBizContent("{" +
                "  \"out_trade_no\":\"4c280b73cbde460eb2ffaa767ed13f2e\"" +
                "}");

//        AlipayTradePayResponse response = alipayClient.execute(request, null, "202308BBd127680e0ea34391a01ac5700eddeF66");

        AlipayTradeQueryResponse response = alipayClient.execute(request, null, "202308BBd127680e0ea34391a01ac5700eddeF66");
        result.setResult(response);
        return result;
    }


    @Autowired
    FkService fkService;

    @GetMapping("getFk")
    public Result<Object> getFk(@RequestParam("cardno") String cardno) {
        Result<Object> result = new Result<>();
        Fk fk = fkService.getOne(new QueryWrapper<Fk>().lambda().eq(Fk::getCardno, cardno));
        result.setResult(fk);

        return result;
    }

}
